/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package session01.module01.three_layer.dal;

import java.sql.Connection;
import java.sql.DriverManager;

/**
 *
 * @author sinhnx <sinhnx@fpt.aptech.ac.vn>
 */
public class DB_Util {

    public static Connection getConnection() {
        Connection cn = null;
        //Create a variable for ClassName
        String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String protocol = "jdbc:sqlserver://";
        String server = "192.168.1.23";
        String port = "1433";
        String dbName = "APJII_DB";
        String login = "sinhnx";
        String password = "fat123456";
        // Create a variable for the connection string.
        StringBuilder sbURL = new StringBuilder(protocol);
        sbURL.append(server).append(":").append(port).append(";databaseName=").append(dbName);
        sbURL.append(";user=").append(login).append(";password=").append(password);
        String url = sbURL.toString();
//        String url = "jdbc:sqlserver://" + server + ":" + port + ";databaseName=" + dbName + ";user=" + login + ";password=" + password;
        try {
            // Establish the connection.
            Class.forName(driver);
            cn = DriverManager.getConnection(url);
        } // Handle any errors that may have occurred.
        catch (Exception e) {
        }
        return cn;
    }

    public static Connection getConnection(String login, String password) {
        Connection cn = null;
        //Create a variable for ClassName
        String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String protocol = "jdbc:sqlserver://";
        String server = "192.168.1.51";
        String port = "1433";
        String dbName = "APJII_DB";
        // Create a variable for the connection string.
        StringBuilder sbURL = new StringBuilder(protocol);
        sbURL.append(server).append(":").append(port).append(";databaseName").append(dbName);
        sbURL.append(";user=").append(login).append(";password=").append(password);
        String url = sbURL.toString();
//        String url = "jdbc:sqlserver://" + server + ":" + port + ";databaseName=" + dbName + ";user=" + login + ";password=" + password;
        try {
            // Establish the connection.
            Class.forName(driver);
            cn = DriverManager.getConnection(url);
        } // Handle any errors that may have occurred.
        catch (Exception e) {
        }
        return cn;
    }

    public static Connection getConnection(String dbName, String login, String password) {
        Connection cn = null;
        //Create a variable for ClassName
        String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String protocol = "jdbc:sqlserver://";
        String server = "192.168.1.51";
        String port = "1433";
        // Create a variable for the connection string.
        StringBuilder sbURL = new StringBuilder(protocol);
        sbURL.append(server).append(":").append(port).append(";databaseName").append(dbName);
        sbURL.append(";user=").append(login).append(";password=").append(password);
        String url = sbURL.toString();
//        String url = "jdbc:sqlserver://" + server + ":" + port + ";databaseName=" + dbName + ";user=" + login + ";password=" + password;
        try {
            // Establish the connection.
            Class.forName(driver);
            cn = DriverManager.getConnection(url);
        } // Handle any errors that may have occurred.
        catch (Exception e) {
        }
        return cn;
    }

    public static Connection getConnection(String server, String dbName, String login, String password) {
        Connection cn = null;
        //Create a variable for ClassName
        String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String protocol = "jdbc:sqlserver://";
        String port = "1433";
        // Create a variable for the connection string.
        StringBuilder sbURL = new StringBuilder(protocol);
        sbURL.append(server).append(":").append(port).append(";databaseName").append(dbName);
        sbURL.append(";user=").append(login).append(";password=").append(password);
        String url = sbURL.toString();
//        String url = "jdbc:sqlserver://" + server + ":" + port + ";databaseName=" + dbName + ";user=" + login + ";password=" + password;
        try {
            // Establish the connection.
            Class.forName(driver);
            cn = DriverManager.getConnection(url);
        } // Handle any errors that may have occurred.
        catch (Exception e) {
        }
        return cn;
    }

    public static Connection getConnection(String server, String port, String dbName, String login, String password) {
        Connection cn = null;
        //Create a variable for ClassName
        String driver = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
        String protocol = "jdbc:sqlserver://";
        // Create a variable for the connection string.
        StringBuilder sbURL = new StringBuilder(protocol);
        sbURL.append(server).append(":").append(port).append(";databaseName").append(dbName);
        sbURL.append(";user=").append(login).append(";password=").append(password);
        String url = sbURL.toString();
//        String url = "jdbc:sqlserver://" + server + ":" + port + ";databaseName=" + dbName + ";user=" + login + ";password=" + password;
        try {
            // Establish the connection.
            Class.forName(driver);
            cn = DriverManager.getConnection(url);
        } // Handle any errors that may have occurred.
        catch (Exception e) {
        }
        return cn;
    }

    public static Connection getConnection(String driver, String protocol, String server, String port, String dbName, String login, String password) {
        Connection cn = null;
        // Create a variable for the connection string.
        StringBuilder sbURL = new StringBuilder(protocol);
        sbURL.append(server).append(":").append(port).append(";databaseName").append(dbName);
        sbURL.append(";user=").append(login).append(";password=").append(password);
        String url = sbURL.toString();
//        String url = "jdbc:sqlserver://" + server + ":" + port + ";databaseName=" + dbName + ";user=" + login + ";password=" + password;
        try {
            // Establish the connection.
            Class.forName(driver);
            cn = DriverManager.getConnection(url);
        } // Handle any errors that may have occurred.
        catch (Exception e) {
        }
        return cn;
    }
}